package com.xiaotu.spider.project.pageprocessor.nettv;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.stream.Stream;
import java.util.Set;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import com.xiaotu.common.exception.SpiderException;
import com.xiaotu.common.exception.SpiderExceptionCode;
import com.xiaotu.common.model.NetTVModel;
import com.xiaotu.common.util.Constants;
import com.xiaotu.common.util.DateUtils;
import com.xiaotu.common.util.GsonUtils;
import com.xiaotu.common.util.SepratorUtil;
import com.xiaotu.common.util.StringUtil;
import com.xiaotu.spider.AUrlPageProcessor;

import net.sf.json.JSONObject;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;

/**
 * 乐视视频最新三年网剧爬取：名称、集数、类型、导演、主演、语言、年份、简介、播放量、评分
 *
 * @auther GaoHJ
 * @create 2017/10/13
 */
@Component("LeTVNetTVPageProcessor")
public class LeTVNetTVPageProcessor extends AUrlPageProcessor
{
	
    private static final String[] YEARS = DateUtils.getRecentYears(3);
    
    private static final String BASE_URL =
            "http://list.le.com/getLesoData?from=pc&src=1&stype=1&ps=30&pn=%s&ph=420001&dt=1&cg=2&or=%s&stt=1&vt=180001&yr=%s&ar=%s&s=1";
    
    private static final String [] AREA = {"50001","50002","50003"};//内地,香港,台湾
    
    private static final String [] SORRT = {"5","4"};//最新,最热
    
    private static final int PAGE_SIZE = 30;
    
    
    /**
     * 初始化要爬取的地址，本程序中为二个地址（对应二个年份）
     *
     * @return
     */
    @Override
    protected List<JSONObject> getStartRequestUrlsAndParams()
    {
    	List<JSONObject> list = new ArrayList<JSONObject>();
    	for(String sort : SORRT){
    		for(String area : AREA){
    			for(String year : YEARS){
    				for(int i=1;i<=PAGE_SIZE;i++){
    					JSONObject target = new JSONObject();
    					target.put("year", year);
    		            target.put(URL_KEY, String.format(BASE_URL,i,sort,year,area));
        				list.add(target);
        			}
    			}
    		}
    	}
        return list;
    }
    
    
    @SuppressWarnings("unchecked")
	@Override
    public void process(Page page)
    {
    	
    	Map<String, Object> tvMap = page.getRequest().getExtras();
    	
        String json = page.getRawText();
        
        if (StringUtils.isEmpty(json))
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        
        Map<String, Object> map = GsonUtils.fromJson(json, Map.class);
        
        Map<String, Object> data = (Map<String, Object>) map.get("data");
        if (data == null)
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        
        List<Map<String, Object>> arrList = (List<Map<String, Object>>) data.get("arr");
        if (arrList == null || arrList.isEmpty())
            throw new SpiderException(SpiderExceptionCode.DataInvalideException);
        
        List<NetTVModel> list = new ArrayList<>();
        
        arrList.forEach(dataMap ->
        {
        	Map<String, String> directorMap = (Map<String, String>) dataMap.get("directory");
        	
        	String director = null;
        	
            if (null != directorMap)
            {
                director = StringUtils.join(directorMap.values(), SepratorUtil.SEP_COMMA_EN);
            }
            
            String actor = StringUtils.EMPTY;
            List<Map<String, String>> aMapList = (List<Map<String, String>>) dataMap.get("starring");
            if (aMapList != null && !aMapList.isEmpty())
            	for(Map<String, String> aMap : aMapList){
            		Set<Entry<String, String>> entrySet = aMap.entrySet();
            		for(Entry<String, String> entry : entrySet){
            			actor += SepratorUtil.SEP_COMMA_EN+entry.getValue();
            		}
            	}

            if(null != actor && StringUtils.isNotBlank(actor)){
            	actor = actor.substring(1);
            }
            
            Long releaseDateLong = dataMap.get("releaseDate") == null?null:Long.parseLong(dataMap.get("releaseDate").toString());
            
            String year = null;
            
            if(null != releaseDateLong && releaseDateLong>0){
            	year = DateUtils.transferLongToDateString(DateUtils.DAY_FORMAT, releaseDateLong);
            }
            
            String name = dataMap.get("name") == null?null:StringUtil.nullToString
                    (dataMap.get("name"));
            
            String episodes = dataMap.get("episodes") == null?null:StringUtil.nullToString
                    (dataMap.get("episodes"));
            
            String subCategoryName = dataMap.get
                    ("subCategoryName") == null?null:StringUtil.nullToString(dataMap.get
                    ("subCategoryName"));
            String areaName = dataMap.get("areaName") == null?null:StringUtil.nullToString(dataMap.get("areaName"));
            
            String description = dataMap.get("description") == null?null:StringUtil
            .nullToString(dataMap.get("description"));
            
            String rating = dataMap.get("rating") == null?null:StringUtil.nullToString(dataMap.get("rating"));
            
            String playCount = dataMap
                    .get("playCount") == null?null:Integer.parseInt(dataMap
                            .get("playCount").toString())+StringUtils.EMPTY;
            
            list.add(new NetTVModel(Constants.TargetType.OTHER_TYPE_ID, name, 
            		Constants.SiteName.LETV
            		,episodes, subCategoryName, null, areaName,
                    year, director, actor, description, playCount, rating, 
                    StringUtil.nullToString(tvMap.get("year"))));
        });

        page.putField(NetTVModel.class.getName() + "List", list);

    }

    @Override
    protected String getSpiderType()
    {
        return "LeTV_NetTV";
    }

    @Override
    public String getPageDataType(Page page)
    {
        return Constants.PageDataType.TV_RECORD;
    }


	@Override
	protected String[] getStartRequestUrls() {
		return null;
	}

}
